package org.skyeye.modeling.view;

import java.io.File;
import java.util.List;

import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.skyeye.modeling.ui.ImageTools;

/**
 * 模拟器控制视图
 * @author Louis
 *
 */
public class CodeExplorerView extends ViewPart {

	public static final String ID = CodeExplorerView.class.getName();
	private TreeViewer treeViewer;
	

	public CodeExplorerView() {
	}
	
	@Override
	public void createPartControl(Composite parent) {
		treeViewer = new TreeViewer(parent);
		treeViewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
		treeViewer.setContentProvider(new FileTreeContentProvider());
		treeViewer.setLabelProvider(new FileTreeLabelProvider());
	}


	@Override
	public void setFocus() {
		treeViewer.getControl().setFocus();
	}

	public TreeViewer getTreeViewer() {
		return treeViewer;
	}

	public void setTreeViewer(TreeViewer treeViewer) {
		this.treeViewer = treeViewer;
	}
	
	private class FileTreeContentProvider implements ITreeContentProvider {
		@Override
		public void dispose() {
			// TODO Auto-generated method stub

		}

		@Override
		public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
			// TODO Auto-generated method stub

		}

		@SuppressWarnings("rawtypes")
		@Override
		public Object[] getChildren(Object element) {
			if (element instanceof File && ((File) element).isDirectory()) {
				return ((File) element).listFiles();
			}
			return new Object[0];
		}

		@SuppressWarnings("rawtypes")
		@Override
		public Object[] getElements(Object inputElement) {
			if (inputElement instanceof List) {
				List list = (List) inputElement;
				return list.toArray();
			} else if(inputElement instanceof File && ((File) inputElement).isDirectory()) {
				return ((File) inputElement).listFiles();
			}
			return null;
		}

		@Override
		public Object getParent(Object arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public boolean hasChildren(Object element) {
			if (element instanceof File && ((File) element).isDirectory()) {
				return true;
			}
			return false;
		}

	}

	private class FileTreeLabelProvider implements ILabelProvider {
		@Override
		public void addListener(ILabelProviderListener arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void dispose() {
			// TODO Auto-generated method stub

		}

		@Override
		public boolean isLabelProperty(Object arg0, String arg1) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public void removeListener(ILabelProviderListener arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public Image getImage(Object element) {
			if (element instanceof File && ((File) element).isDirectory()) {
				return ImageTools.getImage(ImageTools.FOLDER);
			}
			return ImageTools.getImage(ImageTools.FILE);
		}

		@Override
		public String getText(Object element) {
			if (element instanceof File) {
				return ((File) element).getName();
			}
			return "";
		}

	}
	
}
